// LineItem.cpp: implementation of the CLineItem class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LineItem.h"
#include "LineProperties.h"
#include "resourcemakeDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CLineItem,CObject,1)
CLineItem::CLineItem()
{

	m_rcPoints.SetRectEmpty();

	m_crColor = RGB(0,0,0);

	m_pNext = NULL;
	m_Type = SHARP_LINE;
}

CLineItem::~CLineItem()
{

}

void CLineItem::ShowProperties()
{
	CLineProperties *prop=((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_linePropDlg;
	prop->m_pCurrObj=this;

	prop->m_LineWidth.Format("%d",LineWidth);
	((CComboBox*) prop->GetDlgItem(IDC_LINEWIDTH))->SetCurSel(LineWidth-1);
	prop->m_LineColor = LineColor;
	CString str;
	str.Format("%d:%d:%d",GetRValue(LineColor),GetGValue(LineColor),GetBValue(LineColor));
	prop->m_BtnColor.SetWindowText(str);

	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_pCurrPropDlg->ShowWindow(FALSE);
	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_pCurrPropDlg=prop;


	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->iPage=0;
	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->pDialogpro[((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->ipage]->ShowWindow(SW_HIDE);
    ((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->ipage = 3;
	((CTabCtrl*)((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->GetDlgItem(IDC_TAB2))->SetCurSel(3);
	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_pCurrPropDlg->ShowWindow(SW_SHOW);
//	((CTabCtrl*)((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->GetDlgItem(IDC_TAB2))->SetCurSel(1);
//	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_probasedlg->m_paneTree->ShowWindow(FALSE);
//	((CResourcemakeDlg *)AfxGetApp()->m_pMainWnd)->m_pCurrPropDlg->ShowWindow(TRUE);}
}
void CLineItem::Draw(CDC *pDC)
{
	CPen cMyPen;
	CPen* pOldPen;
	//cMyPen.CreatePenIndirect(&this->MyPen);
	cMyPen.CreatePen(LineStyle,LineWidth,LineColor);
	
	pOldPen=pDC->SelectObject(&cMyPen);
	pDC->MoveTo(this->startX,this->startY); 
	pDC->LineTo(this->endX,this->endY); 	
	pDC->SelectObject(pOldPen);
}

void CLineItem::Serialize(CArchive & ar)
{
	CObject::Serialize(ar);
    if(ar.IsLoading())
    {
    }
	else
	{
    }
}

void CLineItem::parseStyleStr(CString str)
{
	vector<CString> vec = this->DStrSlp(str,":");
	int i=0;
	for(vector<CString>::iterator iter = vec.begin();iter!=vec.end();iter++){
		i++;
		if(i==1){
			LineWidth = atoi(*iter);
		}
		if(i==2){
			LineColor = parseColorStr(*iter);
		}
	}
}

CString CLineItem::getStyleStr()
{
	CString str="";
	CString tmp;
	tmp.Format("%d:",LineWidth);
	str+=tmp;
	tmp.Format("(254,%d,%d,%d)",GetRValue(LineColor),GetGValue(LineColor),GetBValue(LineColor));
	str+=tmp;
	return str;
}

CString CLineItem::getPosStr()
{
	CString str;
	int x1 = startX/CDrawElement.dscrx+0.5;
	int x2 = startY/CDrawElement.dscry+0.5;
	int x3 = endX/CDrawElement.dscrx+0.5;
	int x4 = endY/CDrawElement.dscry+0.5;
	str.Format("%d:%d:%d:%d",x1,x2,x3,x4);
	return str;
}

void CLineItem::parsePosStr(CString str)
{
	vector<CString> vec = DStrSlp(str,":");
	int i=0;
	for(vector<CString>::iterator iter = vec.begin();iter!=vec.end();iter++){
		i++;
		if(i==1){
			startX = atoi(*iter)*CDrawElement.dscrx+0.5;
		}
		if(i==2){
			startY = atoi(*iter)*CDrawElement.dscry+0.5;
		}
		if(i==3){
			endX = atoi(*iter)*CDrawElement.dscrx+0.5;
		}
		if(i==4){
			endY = atoi(*iter)*CDrawElement.dscry+0.5;
		}

	}
}
